﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace FileUpload
{
    /// <summary>
    /// FileUpload 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class FileUpload : System.Web.Services.WebService
    {


        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        string pathService = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        string UploadPathPic = string.Format("{0}{1}", System.Web.HttpContext.Current.Server.MapPath("/"), "");
        string UploadPath = string.Format("{0}{1}", System.Web.HttpContext.Current.Server.MapPath("/"), "");
        [WebMethod(Description = "返回文件是否上传成功,限与图片")]
        public string UploadFiles(byte[] fs, string type = "student")
        {
            type = type.ToLower();
            Identify identify = new Identify();
            pathService = pathService.Substring(pathService.IndexOf(':') + 3);
            pathService = pathService.Substring(0, pathService.IndexOf('/'));
            string filename = "";
            identify.Company = type;
            identify.Ship_Name = "000000";
            filename = Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
            UploadPath += refDirectory(identify); //获取绝对路径
            if (!Directory.Exists(UploadPath))    //如果路径没有则创建
            {
                Directory.CreateDirectory(UploadPath);
            }
            string targetDirectory = "";
            string s = "";
            try
            {
                MemoryStream ms = new MemoryStream(fs);
                FileStream saveFile = new FileStream(UploadPath + "/" + filename, FileMode.Create);
                ms.WriteTo(saveFile);
                ms.Close();
                saveFile.Close();
                ms.Dispose();
                saveFile.Dispose();
                if (!string.IsNullOrEmpty(type))
                {
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_800x800.jpg";
                    PTImage.WhiteCutForSquare(UploadPath + "/" + filename, targetDirectory, 800, 800);
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_360x360.jpg";
                    PTImage.WhiteCutForSquare(UploadPath + "/" + filename, targetDirectory, 360, 117);
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_300x300.jpg";
                    PTImage.WhiteCutForSquare(UploadPath + "/" + filename, targetDirectory, 300, 117);
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_200x200.jpg";
                    PTImage.WhiteCutForSquare(UploadPath + "/" + filename, targetDirectory, 200, 117);
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_120x120.jpg";
                    PTImage.WhiteCutForSquare(UploadPath + "/" + filename, targetDirectory, 120, 117);

                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_320x275.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 320, 275, "HW");
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_285x440.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 285, 440, "HW");
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_790x389.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 790, 389, "HW");

                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_90x60.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 90, 60, "HW");
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_600x400.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 600, 400, "HW");
                    targetDirectory = UploadPath + "/" + PTImage.GetFileName(filename) + "_194x124.jpg";
                    PTImage.MakeThumbnail(UploadPath + "/" + filename, targetDirectory, 194, 124, "HW");

                    s = string.Format("{0}{1}{2}{3}{4}{5}", "http://", pathService, "/", refDirectory(identify), "/", PTImage.GetFileName(filename) + "_120x120.jpg");
                }
            }
            catch (ArgumentNullException e)
            {
                return e.Message;
                throw;
            }
            catch (IOException ex)
            {
                return ex.Message;
                throw;
            }
            finally
            {
            }
            return s;
        }
        private string refDirectory(Identify identify)
        {
            return identify.Company + "/" + identify.Ship_Name;
        }

        [WebMethod(Description = "返回文件是否上传成功,上传文件")]
        public string UploadFilesByDocument(byte[] fs, string fileName)
        {
            Identify identify = new Identify();
            pathService = pathService.Substring(pathService.IndexOf(':') + 3);
            pathService = pathService.Substring(0, pathService.IndexOf('/'));

            identify.Company = "File";
            identify.Ship_Name = DateTime.Now.ToString("yyyyMMddHHmmss");

            UploadPath += refDirectory(identify); //获取绝对路径
            if (!Directory.Exists(UploadPath))    //如果路径没有则创建
            {
                Directory.CreateDirectory(UploadPath);
            }
            string s = "";
            try
            {
                MemoryStream ms = new MemoryStream(fs);
                FileStream saveFile = new FileStream(UploadPath + "/" + fileName, FileMode.Create);
                ms.WriteTo(saveFile);
                ms.Close();
                saveFile.Close();
                ms.Dispose();
                saveFile.Dispose();
                s = string.Format("{0}{1}{2}{3}{4}{5}", "http://", pathService, "/", refDirectory(identify), "/", fileName);
            }
            catch (ArgumentNullException e)
            {
                return e.Message;
                throw;
            }
            catch (IOException ex)
            {
                return ex.Message;
                throw;
            }
            finally
            {
            }
            return s;
        }


        /// <summary>
        /// 判断是否图片
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private bool IsPicture(string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                string fileClass;
                byte buffer;
                buffer = reader.ReadByte();
                fileClass = buffer.ToString();
                buffer = reader.ReadByte();
                fileClass += buffer.ToString();
                reader.Close();
                fs.Close();
                if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677")
                //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }


        [WebMethod(Description = "返回文件是否上传成功,上传文件")]
        public string WebUploadFilesByDocument(byte[] fs, string fileName)
        {
            Identify identify = new Identify();
            pathService = pathService.Substring(pathService.IndexOf(':') + 3);
            pathService = pathService.Substring(0, pathService.IndexOf('/'));

            identify.Company = "Web";
            identify.Ship_Name = DateTime.Now.ToString("yyyyMMddHHmmss");

            UploadPath += refDirectory(identify); //获取绝对路径
            if (!Directory.Exists(UploadPath))    //如果路径没有则创建
            {
                Directory.CreateDirectory(UploadPath);
            }
            string s = "";
            try
            {
                MemoryStream ms = new MemoryStream(fs);
                FileStream saveFile = new FileStream(UploadPath + "/" + fileName, FileMode.Create);
                ms.WriteTo(saveFile);
                ms.Close();
                saveFile.Close();
                ms.Dispose();
                saveFile.Dispose();
                s = string.Format("{0}{1}{2}{3}{4}{5}", "http://", pathService, "/", refDirectory(identify), "/", fileName);
            }
            catch (ArgumentNullException e)
            {
                return e.Message;
                throw;
            }
            catch (IOException ex)
            {
                return ex.Message;
                throw;
            }
            finally
            {
            }
            return s;
        }
    }
}
